package com.rh.controller;

import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Department;
import com.rh.entity.Employee;
import com.rh.excrptions.DoException;
import com.rh.service.EmployeeService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@CrossOrigin
@Controller
@ResponseBody

@RequiresRoles(value = {"manager","admin"},logical = Logical.OR)
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工分页
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getEmployeePageJson",method = RequestMethod.GET)
    public Result getEmployeePageJson(Integer pageNo, Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 2;
            }
            Page departmentPage = employeeService.getEmployeePage(pageNo, pageSize);
            Result result = new Result();
            result.setData(departmentPage);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 添加员工
     */
    @RequiresAuthentication
    @RequestMapping(value = "/add_employee",method = RequestMethod.POST)
    public Result addEmployee(Employee employee){
        try{
            employeeService.addMethod(employee);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     * 更新员工
     */
    @RequiresAuthentication
    @RequestMapping(value = "/update_employee",method = RequestMethod.POST)
    public Result updateemployee(Employee employee){
        try{
            System.out.println("更新=====>"+employee);
            employeeService.updateMethod(employee);
            Result result = new Result();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     *  删除部员工
     */
    @RequiresAuthentication
    @RequestMapping(value = "/delete_employee",method = RequestMethod.GET)
    public Result deleteemployee(Integer empId){
        try{
            employeeService.deleteMethod(empId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 根据id查询员工信息
     */
    @RequiresAuthentication
    @RequestMapping(value = "/getemployeeById",method = RequestMethod.GET)
    public Result getemployeeById(Integer empId) {
        try {
            Employee methodAllById = employeeService.getMethodAllById(empId);
            Result result = new Result();
            result.setData(methodAllById);
            return result;
        }catch (Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
}